package com.ghc.util;

import com.sun.mail.util.MailSSLSocketFactory;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;

@Data
@Slf4j
@Component
@RefreshScope
@ConfigurationProperties(prefix = "qq.email")
public class QQMailUtils {

    private String account;
    private String password;

    /**
     *
     * @param to 收件人
     * @return 成功/失败
     */
    public boolean sendMail(String to, String code){
        try {
            //设置发件人
            String from = account;
            //设置邮件发送的服务器，这里为QQ邮件服务器
            String host = "smtp.qq.com";
            //获取系统属性
            Properties properties = System.getProperties();
            //SSL加密
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", sf);
            //设置系统属性
            properties.setProperty("mail.smtp.host", host);
            properties.put("mail.smtp.auth", "true");
            //获取发送邮件会话、获取第三方登录授权码
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(from, password);
                }
            });
            Message message = new MimeMessage(session);
            //防止邮件被当然垃圾邮件处理，披上Outlook的马甲
            message.addHeader("X-Mailer","Microsoft Outlook Express 6.00.2900.2869");
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            //邮件标题
            message.setSubject("www.ghc.com学习使用验证码");
            BodyPart bodyPart = new MimeBodyPart();
            bodyPart.setText("您的验证码为：" + code + "， 有效时间为5分钟。");
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(bodyPart);
            /*//附件
            bodyPart = new MimeBodyPart();
            String fileName = "文件路径";
            DataSource dataSource = new FileDataSource(fileName);
            bodyPart.setDataHandler(new DataHandler(dataSource));
            bodyPart.setFileName("文件显示的名称");
            multipart.addBodyPart(bodyPart);*/
            message.setContent(multipart);
            Transport.send(message);
            log.info("mail transports successfully");
            return true;
        } catch (Exception e) {
            log.warn("邮件发送失败", e);
            return false;
        }
    }

}
